<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发货单号" prop="deviSn">
        <el-input
          v-model="queryParams.deviSn"
          placeholder="请输入发货单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            icon="el-icon-plus"-->
<!--            size="mini"-->
<!--            :disabled="single"-->
<!--            @click="handleAdd()"-->
<!--            v-hasPermi="['mes:deliver:add']"-->
<!--          >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['mes:deliver:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['mes:deliver:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:deliver:export']"
        >导出</el-button>
      </el-col>
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="deliverList"  border style="width: 100%" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" align="center" prop="number" />
      <el-table-column label="发货单号" align="center" prop="deviSn" />
      <el-table-column label="销售单号" align="center" prop="orderSn" />
<!--      <el-table-column label="发货数量" align="center" prop="deviNum" />-->
      <el-table-column label="发货日期" align="center" prop="deliverDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliverDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售员" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:deliver:edit']"
          >申请发货</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:deliver:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改发货通知对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="销售员" prop="createdBy">
<!--          <el-select v-model="form.createdBy" placeholder="请选择销售员" style="width: 15%;" :disabled="true">-->
<!--            <el-option-->
<!--              v-for="item in UserList"-->
<!--              :key="item.userId"-->
<!--              :label="item.userName"-->
<!--              :value="item.userName">-->
<!--            </el-option>-->
<!--          </el-select>-->
          <el-input v-model="form.createdBy" placeholder="请输入" style="width: 15%;" :disabled="true" />
        </el-form-item>
        <el-form-item label="客户名称" prop="custId">
          <el-select v-model="form.custId" placeholder="请选择客户" style="width: 15%;" @change="(option) => changeCust(option)" :disabled="true">
            <el-option
              v-for="item in custList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="title=='确认发货'" label="联系电话" prop="contactNumber">
          <el-input v-model="form.contactNumber" placeholder="请输入" style="width: 15%;" :disabled="true" />
        </el-form-item>
        <el-form-item v-if="title=='确认发货'" label="收货地址" prop="address">
          <el-input v-model="form.address" type = "textarea" placeholder="请输入" style="width: 15%;" :disabled="true" />
        </el-form-item>
        <el-form-item label="发货日期" prop="deliverDate">
          <el-date-picker clearable
                          v-model="form.deliverDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          style="width: 15%;"
                          placeholder="请选择发货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货产品" >
          <el-card shadow="always" style="border-radius: 15px;">
            <el-table style="border-radius: 15px;" v-loading="loading" border :data="deliverProList">
              <el-table-column label="销售单号" align="center" prop="order" min-width="170">
                <template slot-scope="scope">
                  <span>{{scope.row.orderSn}}</span>
                </template>
              </el-table-column>
              <el-table-column label="签订日期" align="center" prop="signDate" min-width="100">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.signDate, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="交货日期" align="center" prop="deliverDate" min-width="100">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.deliverDate, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="产品编码" align="center" prop="proSn" min-width="170"/>
              <el-table-column label="产品名称" align="center" prop="proName" min-width="80">
              </el-table-column>
              <el-table-column label="需求数量" align="center" class-name="small-padding fixed-width"  min-width="60">
                <template slot-scope="scope">
                  <span>{{scope.row.deviNum}}</span>
                </template>
              </el-table-column>
              <el-table-column label="已发货数量" align="center" class-name="small-padding fixed-width"  min-width="90">
                <template slot-scope="scope">
                  <span>{{ scope.row.deliverNum+0 }}</span>
                </template>
              </el-table-column>
<!--              <el-table-column label="已出库数量" align="center" class-name="small-padding fixed-width"  min-width="90">-->
<!--                <template slot-scope="scope">-->
<!--                  <span>{{ scope.row.strmNum }}</span>-->
<!--                </template>-->
<!--              </el-table-column>-->
              <el-table-column label="未发货数量" align="center" class-name="small-padding fixed-width"  min-width="90">
                <template slot-scope="scope">
                  <span>{{ scope.row.deviNum-scope.row.deliverNum }}</span>
                </template>
              </el-table-column>
<!--              <el-table-column label="当前库存" align="center" class-name="small-padding fixed-width">-->
<!--                <template slot-scope="scope">-->
<!--                  <span>{{scope.row.proNum}}</span>-->
<!--                </template>-->
<!--              </el-table-column>-->
<!--              <el-table-column label="存货单位" align="center" prop="unitName"/>-->
              <el-table-column label="本次发货数量" prop="deliver_num" align="center" class-name="small-padding fixed-width"  width="220px">
                <template slot-scope="scope">
                  <el-input-number id="inputNumber" v-model:value="scope.row.nowDeliverNum " :min="0" :max="scope.row.deviNum-scope.row.deliverNum " :precision="2"/>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button size="mini" type="text" icon="el-icon-edit" @click="submitForm(scope.row)"
                             v-hasPermi="['mes:proinfo:edit']">发货</el-button>
                </template>
              </el-table-column>
            </el-table>

          </el-card>
        </el-form-item>
      </el-form>
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitForm">发货</el-button>-->
<!--        <el-button @click="cancel">取 消</el-button>-->
<!--      </div>-->
    </el-dialog>
  </div>
</template>

<script>
import { listDeliver, getDeliver, delDeliver, addDeliver, updateDeliver, getDeliverPro } from "@/api/mes/deliver";
import {listCust_info} from "@/api/mes/cust_info";
import {listUser} from "@/api/system/user";

export default {
  name: "Deliver",
  data() {
    return {
      //未发货数量
      undeliverNumList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中销售订单数组
      orderid: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发货通知表格数据
      deliverList: [],
      //客户信息表数据
      custList:[],
      //销售员信息表数据
      UserList:[],
      // 发货产品表格数据
      deliverProList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviSn: null,
      },
      // 客户信息查询参数
      queryParamsCust: {
        sn: null,
        name: null,
      },
      // 客户信息查询参数
      queryParamsUser: {
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined
      },
      // 发货产品信息查询参数
      queryParamsPro: {
        orderId: null,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getCustList();
    this.getUserList()
  },
  methods: {
    /**
     * 下拉框改变
     * @param {选项值} option
     */
    changeCust(option) {
      let ss = this.custList.find(item => item.id == option)
      this.form.contactNumber=ss.contactNumber;
      this.form.address=ss.address;
      console.log(this.matOptions,"物料数组")
    },
    /** 查询销售员信息列表 */
    getUserList() {
      listUser(this.queryParamsUser).then(response => {
        this.UserList = response.rows;
        console.log("客户信息列表", this.UserList)
      });
    },
    /** 查询客户信息列表 */
    getCustList() {
      listCust_info(this.queryParamsCust).then(response => {
        this.custList = response.rows;
        console.log("客户信息列表", this.custList)
      });
    },
    /** 查询发货通知列表 */
    getList() {
      this.loading = true;
      listDeliver(this.queryParams).then(response => {
        this.deliverList = response.rows;
        console.log("685854698489",this.deliverList)
        this.total = response.total;
        this.loading = false;
        this.calculateIndex()
      });
    },
    /** 计算列表编号 */
    calculateIndex() {
      this.deliverList.forEach((item, index) => {
        this.$set(item, 'number', index + 1);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        dfsId: null,
        custId:null,
        userId:null,
        proId: null,
        deviSn: null,
        deviNum: null,
        isDeleted: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedDate: null,
        remark: null,
        orderLst:[],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.orderid = selection.map(item => item.orderId)
      console.log(this.orderid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // /** 新增按钮操作 */
    // handleAdd(row) {
    //   this.queryParamsPro.orderId=this.orderid[0]
    //   getDeliverPro(this.queryParamsPro).then(response => {
    //     this.deliverProList = response.rows;
    //     this.open = true;
    //     this.title = "添加发货通知";
    //   });
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.queryParamsPro.orderId= row.orderId ||this.orderid[0]
      getDeliver(id).then(response => {
        this.form = response.data;
        getDeliverPro(this.queryParamsPro).then(response => {
          console.log("response.rows",response.rows)
          this.deliverProList = response.rows;
          this.deliverProList.forEach((item)=>{
            item.custId=parseInt(item.custId);
            item.nowDeliverNum=0;
          })
          this.form.custId=this.deliverProList[0].custId;
          this.form.contactNumber=this.deliverProList[0].contactNumber;
          this.form.address=this.deliverProList[0].address;

          console.log("this.form",this.form)
        });
        this.open = true;
        this.title = "确认发货";
      });
    },
    /** 提交按钮 */
    submitForm(row) {
      row.deliverDate=this.form.deliverDate
      row.deliverNum=row.deliverNum+row.nowDeliverNum;
      console.log("row",row)
      updateDeliver(row).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != null) {
      //     } else {
      //       addDeliver(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除发货通知编号为"' + ids + '"的数据项？').then(function() {
        return delDeliver(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/deliver/export', {
        ...this.queryParams
      }, `deliver_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
